package cn.itcast.bos.web.action.take_delivery;

import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.take_delivery.WayBillService;
import cn.itcast.bos.web.action.common.BaseAction;
import com.alibaba.fastjson.JSONArray;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@Namespace("/")
@ParentPackage("struts-default")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class WayBillAction extends BaseAction<WayBill> {

    private static final Logger LOG = Logger.getLogger(WayBillAction.class);
    @Autowired
    private WayBillService wayBillService;

    @Action(value = "wayBill_save")
    public void waybillSave() {
        Map<String, Object> mapData = new HashMap<>();
        try {
            if (model.getOrder() != null && (model.getOrder().getId() == null || model.getOrder().getId() == 0)) {
                model.setOrder(null);
            }
            wayBillService.save(model);
            mapData.put("success", true);
            mapData.put("msg", "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            mapData.put("success", false);
            mapData.put("msg", "添加失败");
        }


        String json = JSONArray.toJSONString(mapData);
        ServletActionContext.getResponse().setCharacterEncoding("utf-8");
        try {
            ServletActionContext.getResponse().getWriter().write(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Action(value = "wayBill_showPage")
    public void showPage() {
        Pageable pageable = new PageRequest(page, rows, Sort.Direction.DESC, "id");
        Page<WayBill> pageData = wayBillService.showPage(pageable,model);
        writeJson(pageData);
    }

    @Action(value = "wayBill_findByNum")
    public void findByNum() {
        Map<String, Object> mapData = new HashMap<>();
        WayBill wayBill = wayBillService.findByNum(model.getWayBillNum());
        if (wayBill != null) {
            mapData.put("success", true);
            mapData.put("wayBillData", wayBill);
        } else {
            mapData.put("success", false);
        }
        String json = JSONArray.toJSONString(mapData);
        ServletActionContext.getResponse().setCharacterEncoding("utf-8");
        try {
            ServletActionContext.getResponse().getWriter().write(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
